package com.openx.xoj.judge.codesandbok.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.openx.xoj.common.ErrorCode;
import com.openx.xoj.exception.BusinessException;
import com.openx.xoj.judge.codesandbok.CodeSandbox;
import com.openx.xoj.judge.codesandbok.mode.ExecuteCodeRequest;
import com.openx.xoj.judge.codesandbok.mode.ExecuteCodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

/**
 * 远程代码沙箱(调用接口的沙箱)
 */
@Slf4j
public class RemoteCodeSandbox implements CodeSandbox {

    private static final String URL = "http://59.37.95.157:3040/codesandbox/execute";

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        log.info("executeCodeRequest: {}", executeCodeRequest);
        executeCodeRequest.setLanguage(executeCodeRequest.getLanguage().toUpperCase());//转为大写与沙箱对应
        String responseStr = HttpUtil.post(URL, JSONUtil.toJsonStr(executeCodeRequest));
        log.info("responseStr: {}", responseStr);
        return JSONUtil.toBean(responseStr, ExecuteCodeResponse.class);
    }
}
